package com.manage.action;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.manage.po.Borrow;
import com.manage.po.Device;
import com.manage.service.IBorrowService;
import com.manage.service.IDeviceService;
import com.manage.util.PagerModel;

@Controller
@Results({   
		@Result(name = "borrow_list", location = "systemAdmin/borrow/borrowManage.jsp"),
		@Result(name = "borrow_edit", location = "systemAdmin/borrow/borrowEdit.jsp"),
		@Result(name = "error", location = "error.jsp") })
public class BorrowAction extends BaseAction{
	
	private static final long serialVersionUID = 6718838822334455667L;
	@Autowired
	private IBorrowService borrowService;
	@Autowired
	private IDeviceService deviceService;
	
	private PagerModel pm;
	private int pageNum = 1;
	private int numPerPage = 20;
	private Integer[] ids;   //删除ID

	private int borrowid;
	//查找内容
	private String searchBorrowId;
	private String searchDeviceId;
	private String searchBorunitType;
	private String searchReturned;
	private String searchContact;
		
	private String borrow_id;   //修改时，使用到的id 
	private String editorType;  //记录操作的类型，是增删改查 
	
	private Borrow borrow;

    ////////////////////////////////////////////////////////////////////////////////
		
	@Action("borrowmanage")
	public String getBorrowList(){
		pm=borrowService.getBorrowById(borrowid, pageNum, numPerPage);
		return "borrow_list";
	}
	
	@Action("SearchBorrow_List")
	public String search(){
		System.out.println(searchBorrowId + "***" + searchDeviceId + "***" + searchBorunitType + "***" +searchReturned);	
		pm = borrowService.search(searchBorrowId,searchDeviceId, searchBorunitType, searchReturned, searchContact, pageNum, numPerPage);
		
		return "borrow_list";
	}
	
	
	// 用于查找带回
//	@Action("Team_List_LookUp")
//	public String team_list_lookup(){
//		pm = teamService.search(searchId, searchName, searchTeacher,searchRoom, pageNum, numPerPage);
//		return "team_list_lookup";
//	}
//	
	
	//////////////////////////////////////////////////////////
	@Action("Borrow_Add")
	public String toAddBorrowInfo() {
		editorType="add";
		return "borrow_edit";
	}
	
		
	@Action("Borrow_Modify")
	public String showModify(){
		System.out.println("---------777777777----------");
		editorType="modify";
		borrow = borrowService.getBorrowById(Integer.parseInt(borrow_id));
		return "borrow_edit";	
	}

	@Action("Borrow_Save")
	public String save(){

		
	//	System.out.println("_______deviceId"+borrow.getDeviceid());
		
		if("add".equals(editorType)){	//这里表示添加数据	
//			if("0".equals(borrow.getBehavior())){  //这里表示借出
				Device dev=deviceService.getDeviceById(borrow.getDeviceid());
				int left=Integer.parseInt(dev.getCount()) - Integer.parseInt(borrow.getBorcount() );
				if(left<0){
					return ajaxJsonWarningMessage("对不起，您设备剩余数量不足");
				}
				dev.setCount(left+"");
				deviceService.update(dev);
//			}else{			//这里表示归还
//				System.out.println("____else_______deviceId"+borrow.getDeviceid());
//				Device dev=deviceService.getDeviceById(borrow.getDeviceid());
//				dev.setCount((Integer.parseInt(dev.getCount()) + Integer.parseInt(borrow.getBorcount() ))+"");
//				deviceService.update(dev);
//			}

			borrowService.save(borrow);
				
			return ajaxJsonSuccessMessage("您已经添加成功!","BORROWMANAGE","closeCurrent");
		} else{	//这里表示修改数据
//			if("0".equals(borrow.getBehavior())){//这里表示借出
//				Borrow borr=borrowService.getBorrowById(borrow.getBorrowid());//表示上次借设备的情况
//				Device dev=deviceService.getDeviceById(borrow.getDeviceid());  
//				// 设备剩余数量 = 上次设备剩余数量  + 上次借设备的数量 -这次借设备数量
//				int left= Integer.parseInt(dev.getCount()) + Integer.parseInt(borr.getBorcount()) - Integer.parseInt(borrow.getBorcount()) ;
//				dev.setCount(left+"");
//				deviceService.update(dev);		
//			}else{	//这里表示归还
//				System.out.println("____else_______deviceId"+borrow.getDeviceid());
				if(Integer.parseInt(borrow.getBorcount())<Integer.parseInt(borrow.getRetcount())){
					return ajaxJsonWarningMessage("归还数量不能大于借出数量！");
				}
			
				Borrow borr=borrowService.getBorrowById(borrow.getBorrowid());  //上次归还设备情况
				Device dev=deviceService.getDeviceById(borrow.getDeviceid());
				// 设备剩余数量 = 上次设备剩余数量 - 上次归还数量 + 这次归还数量。
				
				int left= Integer.parseInt(dev.getCount()) + Integer.parseInt(borrow.getRetcount()) ;
				//若是修改上次归还数量，则要减去已归还数量。
				if(null!=borr.getRetcount()&&!"".equals(borr.getRetcount()) ){
					left=left-Integer.parseInt(borr.getRetcount());
				}				
				
				dev.setCount(left+"");
				deviceService.update(dev);		
//			}
			borrowService.merge(borrow);
			return ajaxJsonSuccessMessage("您已经修改成功!","BORROWMANAGE","closeCurrent");
		}
		
		
	}
	
	///////////////////////////////////////////////////////////////	
	@Action("Borrow_BatchDelete")
	public String delete(){
		for(Integer i:ids){
			Borrow borrow=borrowService.getBorrowById(i);
			Device dev= deviceService.getDeviceById(borrow.getDeviceid());
			Integer left=Integer.parseInt(dev.getCount() ) + Integer.parseInt(borrow.getBorcount() ) ;
			if(null!=borrow.getRetcount()&&!"".equals(borrow.getRetcount())){
				left= left -Integer.parseInt(borrow.getRetcount());
			}
			
			dev.setCount(left+"");
			deviceService.update(dev);
		}
		borrowService.batchDelete(ids);
		return ajaxJsonSuccessMessage("删除成功","","");
	}

	public IDeviceService getDeviceService() {
		return deviceService;
	}

	public void setDeviceService(IDeviceService deviceService) {
		this.deviceService = deviceService;
	}

	public IBorrowService getBorrowService() {
		return borrowService;
	}

	public void setBorrowService(IBorrowService borrowService) {
		this.borrowService = borrowService;
	}

	public PagerModel getPm() {
		return pm;
	}

	public void setPm(PagerModel pm) {
		this.pm = pm;
	}

	public int getPageNum() {
		return pageNum;
	}

	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}

	public int getNumPerPage() {
		return numPerPage;
	}

	public void setNumPerPage(int numPerPage) {
		this.numPerPage = numPerPage;
	}

	public Integer[] getIds() {
		return ids;
	}

	public void setIds(Integer[] ids) {
		this.ids = ids;
	}

	public int getBorrowid() {
		return borrowid;
	}

	public void setBorrowid(int borrowid) {
		this.borrowid = borrowid;
	}

	public String getSearchBorrowId() {
		return searchBorrowId;
	}

	public void setSearchBorrowId(String searchBorrowId) {
		this.searchBorrowId = searchBorrowId;
	}
	
	public String getSearchReturned() {
		return searchReturned;
	}

	public void setSearchReturned(String searchReturned) {
		this.searchReturned = searchReturned;
	}

	public String getSearchDeviceId() {
		return searchDeviceId;
	}

	public void setSearchDeviceId(String searchDeviceId) {
		this.searchDeviceId = searchDeviceId;
	}

	public String getSearchBorunitType() {
		return searchBorunitType;
	}

	public void setSearchBorunitType(String searchBorunitType) {
		this.searchBorunitType = searchBorunitType;
	}

	public String getBorrow_id() {
		return borrow_id;
	}

	public void setBorrow_id(String borrow_id) {
		this.borrow_id = borrow_id;
	}

	public String getEditorType() {
		return editorType;
	}

	public void setEditorType(String editorType) {
		this.editorType = editorType;
	}

	public Borrow getBorrow() {
		return borrow;
	}

	public void setBorrow(Borrow borrow) {
		this.borrow = borrow;
	}

	public String getSearchContact() {
		return searchContact;
	}

	public void setSearchContact(String searchContact) {
		this.searchContact = searchContact;
	}

	////////////////////////////////////////////////////////////////////////////////

}
